<?php
class Workflow_Node_UserTask extends Workflow_Node
{
	public function __construct( $configuration = null )
    {
    	 parent::__construct( $configuration );
    }
    
	/**
     * Executes this node.
     *
     * @param Workflow_Execution $execution
     * @return boolean true when the node finished execution,
     *                 and false otherwise
     * @ignore
     */
    public function execute( Workflow_Execution_Database $execution )
    {        
    	$svc = new Workflow_Service_UserTask($execution->getDb());
    	
        $isTaskCompleted = $svc->isTaskCompleted($execution->workflow->id, $execution->getId(), $this->id);
        
        if ( $isTaskCompleted )
        {
            $this->activateNode( $execution, $this->outNodes[0] );

            return parent::execute( $execution );
        }
        else
        {
        	$svc->addWaitingTask($execution->workflow->id, $execution->getId(), $this->id);
        	
        	if ($this->id != $execution->workflow->startNode->getFirstOutNode()->getId())
        	{
        		$svc->setWorkItemCandidates($execution->workflow->id, $execution->getId(), $this);        		
        	}
        	
            return false;
        }
    }
    
	/**
     * Generate node configuration from XML representation.
     *
     * @param DOMElement $element
     * @return array
     * @ignore
     */
    public static function configurationFromXML( DOMElement $element )
    {
        $configuration = array();
		
        //task user
        $taskuser = $element->getElementsByTagName('taskuser')->item(0);
        if ($taskuser) 
        {
	        $configuration['UserRetriever'] = $taskuser->getAttribute('class');
	        
	    	$taskuserArgs = Workflow_Util::getChildNode($taskuser);
	
	        if ( $taskuserArgs->tagName == 'arguments' )
	        {
	            foreach ( Workflow_Util::getChildNodes( $taskuserArgs ) as $argument )
	            {
	                $configuration['UserRetrieverArgs'][] = Workflow_DefinitionStorage_Xml::xmlToVariable( $argument );
	            }
        	}
        }
        
        //task render
        $taskrender = $element->getElementsByTagName('taskrender')->item(0);
        if ($taskrender)
        {
        	$configuration['Render'] = $taskrender->getAttribute('class');
        	$rendercontent = $taskrender->getElementsByTagName('rendercontent')->item(0);
        	foreach ($rendercontent->childNodes as $n)
        	{
        		if ($n->nodeType == XML_CDATA_SECTION_NODE)
        		{
        			$configuration['RenderContent'] = $n->data;
        		}
        	}        		
        }
        

        return $configuration;
    }

    /**
     * Generate XML representation of this node's configuration.
     *
     * @param DOMElement $element
     * @ignore
     */
    public function configurationToXML( DOMElement $element )
    {
    	//task user
        $taskuser = $element->appendChild($element->ownerDocument->createElement('taskuser' ));
        $taskuser->setAttribute( 'class', $this->configuration['UserRetriever'] );
    	if ( !empty( $this->configuration['UserRetrieverArgs'] ) )
        {
            $taskuserArgs = $taskuser->appendChild(
              $taskuser->ownerDocument->createElement( 'arguments' )
            );

            foreach ( $this->configuration['UserRetrieverArgs'] as $argument )
            {
                $taskuserArgs->appendChild(
                  Workflow_DefinitionStorage_Xml::variableToXml(
                    $argument, $element->ownerDocument
                  )
                );
            }
        }  

        //task render
        $taskrender = $element->appendChild($element->ownerDocument->createElement('taskrender' ));
        $taskrender->setAttribute( 'class', $this->configuration['Render'] );
        $rendercontent = $taskrender->ownerDocument->createElement('rendercontent');
        
        $cddata = $rendercontent->ownerDocument->createCDATASection($this->configuration['RenderContent']);
        
        $rendercontent->appendChild($cddata);
        
        $taskrender->appendChild($rendercontent);
    }   
}
?>